package com.winit.schedule.job.tracker.util;

import com.google.gson.Gson;
import com.winit.schedule.core.repository.entity.UserEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jianke.zhang on 2017/5/4.
 */
public class ZentaoUtils {

    private static final Logger logger = LoggerFactory.getLogger(ZentaoUtils.class);

    public static UserEntity login(String username, String password, String loginUri) {
        CloseableHttpClient client = HttpClients.createDefault();;
        HttpPost post = new HttpPost(loginUri);
        List<NameValuePair> parms = new ArrayList<NameValuePair>();
        parms.add(new BasicNameValuePair("account", username));
        parms.add(new BasicNameValuePair("password", password));
        UrlEncodedFormEntity entity;
        UserEntity user = null;
        try {
            entity = new UrlEncodedFormEntity(parms, "utf-8");
            post.setEntity(entity);
            logger.info("executing request " + post.getURI());
            HttpResponse response = client.execute(post);


            if (200 == response.getStatusLine().getStatusCode()) {
                HttpEntity entity2 = response.getEntity();
                if (entity2 != null) {
                    logger.info("--------------------------------------");
                    String content = EntityUtils.toString(entity2, "utf-8");
                    logger.info("Response content: " + content);
                    logger.info("--------------------------------------");
                    logger.info(response.getStatusLine().toString());

                    Gson gson = new Gson();
                    Map<String, Object> data = gson.fromJson(content, HashMap.class);
                    if ("success".equalsIgnoreCase(data.get("status").toString())) {
                        logger.info("-------------------------- > login success.");
                        user = new UserEntity();
                        user.setUsername(username);
                        user.setPassword(password);

                        Map<String, Object> info = (Map<String, Object>)data.get("user");
                        user.setRemark(null != info && null != info.get("realname") ? info.get("realname").toString() : null);
                        user.setDisabled(true);
                        user.setAdmin(false);

                    }
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
            }
        }
        return user;
    }

}
